<?php

namespace app\api\controller\company;
use app\shop\model\company\CompanyFile;
use app\shop\model\company\Station;
use app\shop\model\company\WorkReport;
use app\common\service\Result;
/**
 * 团队风采
 */
class File
{

    public function list()
    {
        $type = input("type");
        $app_id = request()->app_id;
        $map = [
            ['app_id','=',$app_id]
        ];
        $lists = [];
        switch($type){
            case "1":
            case "2":
                $map[] = ['file_type','=',$type];
                $department_id = input("department_id",0);
                if($department_id){
                    $map[] = ['department_id','=',$department_id];
                }
                $lists = CompanyFile::where($map)->order('sort desc,id desc')->field('file_title,file_path,create_time')->select()->toArray();
                break;
            case "station":     //岗位说明说
                $lists = Station::where($map)->whereNotNull('file')->order('sort desc,id desc')->field('title as file_title,file as file_path')->select()->toArray();
                break;
            case "report":      //工作报告
                $res = WorkReport::where($map)->order('id desc')->with(['user'])->append(['type_name'])->select()->toArray();
                foreach($res as $v){
                    $title = $v['title']."_".$v['type_name']."_".$v['user']['real_name'];
                    $lists[] = [
                        'file_title' => $title,
                        'file_path' => $v['file'],
                        'create_time' => $v['create_time'],
                    ];
                }
                break;
            case "others":
                $map[] = ['file_type','>',2];
                $department_id = input("department_id",0);
                if($department_id){
                    $map[] = ['department_id','=',$department_id];
                }
                $lists = CompanyFile::where($map)->order('sort desc,id desc')->field('file_title,file_path,create_time')->select()->toArray();
                break;
        }
        return Result::success($lists);
    }
    
    public function addFile(){
        try{
            $data = input("post.");
            $type = input("type");
            $app_id = request()->app_id;
            switch($type){
                case "1":
                case "2":
                case "3":
                    $data['app_id'] = request()->app_id;
                    $data['file_title'] = $data['title']??($data['file_name']??"未命名文件");
                    $data['add_user'] = request()->user_id;
                    $data['file_type'] = $type;
                    CompanyFile::create($data);
                    break;
                case "station":
                    $station_id = $data['station_id']??0;
                    $station = Station::where(['id'=>$station_id,'app_id'=>$app_id])->find();
                    if($station){
                        $station->file = $data['file_path'];
                        $station->save();
                    }
                    else{
                        return Result::error("岗位不存在");
                    }
                    break;
                case "others":
                    $data['app_id'] = request()->app_id;
                    $data['file_title'] = $data['title']??($data['file_name']??"未命名文件");
                    $data['add_user'] = request()->user_id;
                    $data['file_type'] = 3;
                    CompanyFile::create($data);
                    break;
            }
            return Result::success();
        }
        catch(\Exception $e){
            return Result::error($e->getMessage());
        }
    }
}